package org.xx.armory.commons;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;

import static org.xx.armory.commons.Validators.rejectIfNull;

public final class LocalDateUtils {
    private static final Date MIN_DATE = new Date(0L);
    private static final Date MAX_DATE = new Date(32503651200000L);

    private LocalDateUtils() {
        throw new AssertionError();
    }

    public static Date minDate() {
        return new Date(MIN_DATE.getTime());
    }

    public static Date maxDate() {
        return new Date(MAX_DATE.getTime());
    }

    public static Date buildDate(
            int year,
            Month month,
            int dayOfMonth
    ) {
        return buildDate(year, month, dayOfMonth, ZoneId.systemDefault());
    }

    public static Date buildDate(
            int year,
            Month month,
            int dayOfMonth,
            ZoneId zoneId
    ) {
        rejectIfNull(zoneId, "zoneId");

        return Date.from(LocalDate.of(year, month, dayOfMonth).atStartOfDay().atZone(zoneId).toInstant());
    }

    public static Date buildDateTime(
            int year,
            Month month,
            int dayOfMonth,
            int hour,
            int minute,
            int second
    ) {
        return buildDateTime(year, month, dayOfMonth, hour, minute, second, ZoneId.systemDefault());
    }

    public static Date buildDateTime(
            int year,
            Month month,
            int dayOfMonth,
            int hour,
            int minute,
            int second,
            ZoneId zoneId
    ) {
        rejectIfNull(zoneId, "zoneId");

        return Date.from(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second).atZone(zoneId).toInstant());
    }

    public static Date buildDateUTC(
            int year,
            Month month,
            int dayOfMonth
    ) {
        return buildDate(year, month, dayOfMonth, ZoneOffset.UTC);
    }

    public static Date buildDateTimeUTC(
            int year,
            Month month,
            int dayOfMonth,
            int hour,
            int minute,
            int second
    ) {
        return buildDateTime(year, month, dayOfMonth, hour, minute, second, ZoneOffset.UTC);
    }
}
